package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.imgedit.view.imagezoom.ImageViewTouchBase;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityPicHandleBinding;
import stark.common.apis.ApiManager;
import stark.common.base.IReqRetCallback;
import stark.common.basic.constant.Extra;
import stark.common.basic.utils.UriUtil;

public class PicHandleActivity extends BaseAc<ActivityPicHandleBinding> {

    public static String imgPath = "";
    public static int type;

    @Override
    protected int onCreate() {
        return R.layout.activity_pic_handle;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPicHandleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    @Override
    protected void initData() {
        if (TextUtils.isEmpty(imgPath)) return;
        Glide.with(this).load(imgPath).into(mDataBinding.ivPicHandleImg);
        mDataBinding.ivPicHandleImg.setDisplayType(ImageViewTouchBase.DisplayType.FIT_TO_SCREEN);
        mDataBinding.ivPicHandleImg.setScaleEnabled(false);// 禁用缩放
        //先启动动画，2s后在进行识别
        startAnima();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                distHuman();
            }
        }, 2000);
    }


    private void startAnima() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mDataBinding.ivPicHandleScanLine,
                "translationY", 0, 1200f, 0, 1200f);
        objectAnimator.setDuration(5000);
        objectAnimator.setRepeatCount(ValueAnimator.INFINITE);
        objectAnimator.start();
    }

    //识别人像跳转证件照界面
    private void distHuman() {
        ApiManager.humanApi().hmBodySegBmp(this, UriUtil.path2Uri(mContext, imgPath), new IReqRetCallback<Bitmap>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Bitmap result) {
                if (success) {
                    if (result != null) {
                        if (type == 0) {
                            FigureActivity.bitmap = result;
                            startActivity(FigureActivity.class);
                            finish();
                        } else if (type == 1) {
                            FigureActivity.bitmap = result;
                            Intent intent = new Intent();
                            setResult(RESULT_OK, intent);
                            onBackPressed();
                        }
                    } else {
                        ToastUtils.showShort(getString(R.string.no_dist_human));
                    }
                } else {
                    ToastUtils.showShort(getString(R.string.no_dist_human));
                }
            }
        });
    }

}